<template>
  <van-pull-refresh v-model="refreshing" @refresh="OnRefresh">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="OnLoad"
    >
      <div class="list">
        <div class="item" v-for="item in list" :key="item.id">
          <p>{{ item.content }}</p>
          <p>金额：{{ item.total }}</p>
          <p>时间：{{ item.createtime_date }}</p>
        </div>

        <van-back-top :bottom="'80'" />
      </div>
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
// this代理
const { proxy } = getCurrentInstance();
// 用户数据
const business = proxy.$business;
// 存放数据
const list = ref([]);
// 刷新状态
const refreshing = ref(false);
// 懒加载状态
const loading = ref(false);
// 上拉加载状态
const finished = ref(false);
// 当前页
const page = ref(1);

// 挂载前钩子
onBeforeMount(() => OnRefresh());

// 下拉刷新
const OnRefresh = () => {
  page.value = 1;
  list.value = [];
  finished.value = false;
  loading.value = true;
  refreshing.value = true;

  setTimeout(OnLoad, 1000);
};

// 上拉加载
const OnLoad = async () => {
  // 如果是下拉刷新，重置其值
  if (refreshing.value === true) {
    refreshing.value = false;
  }
  // 组装参数
  let params = {
    page: page.value,
    busid: business.value.id,
  };
  // 发起请求
  let result = await proxy.$http.post({
    url: "/business/record",
    params,
  });
  // 取消加载
  loading.value = false;
  // 处理请求结果
  if (result.code === 1) {
    // 获取数据总数
    let count = result.data ? result.data.count : 0;
    // 获取数据并且合并数组
    list.value = result.data ? list.value.concat(result.data.list) : [];
    // 判断是否加载完所有数据
    if (list.value.length >= count) {
      // 完成加载
      finished.value = true;
      return false;
    }
    // 分页+1
    page.value = page.value + 1;
  } else {
    finished.value = true;
  }
};
</script>

<style scoped>
.item {
  background: #fff;
  width: 100%;
  font-size: 1em;
  padding: 15px 0;
  border-bottom: 1px solid #eaeaea;
}

.item p {
  padding: 0 15px;
}

.van-pull-refresh {
  min-height: 90vh;
}
</style>